package org.Aftpserver;

import java.io.IOException;
import java.net.*;
import java.util.*;

import org.aftputil.AFtpLogger;
/**
 *  "AFtpServerThread" means "Adaptive FtpServer". AAftp server is Adaptive server which is created for LAN.
 *  In LAN ,the IP address is offen assigned dynamicaly,if you have got the experience of deploying
 *  a ftp server In LAN,you must have been bothered by problen of changing IP. AFtpServerThread can handle this
 *  problem and help you out. 
 *  More infomation mail buptjunjun@gmail.com
 * @author andy yang 
 * @version 1.0
 */
public class AFtpServerThread extends Thread
{

	/**
	 * control flag
	 */
	private boolean flag = true;
	
	/**
	 *  default listening port 
	 */
	private int port = 21;
	
	/**
	 *  the length of waiting queue  for the listenSocket
	 */
	private int backLog = 10;
	
	/**
	 *  root directory
	 */
	private static String root = "";
	
	/**
	 *  the socket Listening to the coming Connection
	 */
	ServerSocket listenSocket = null;
	
	/**
	 *  store the handle of connection Thread
	 */
	List lthread = null;
	
	

	
	/**
	 * init AFtpServerThread
	 * @param port listening port
	 * @param root root directory
	 * @throws IOException
	 */
	public AFtpServerThread(int port, int backLog,String root)  throws IOException
	{
		
		this.port = port;
		this.root = root;
		this.backLog = backLog;
		this.lthread = new ArrayList<Thread>();
		this.listenSocket = new ServerSocket(this.port,this.backLog);
		System.out.println("root :" + root);
	}
	

	/**
	 * simplely init AFtpServerThread
	 * @param root root directory
	 * @throws IOException
	 */
	public AFtpServerThread(String root)  throws IOException
	{
		this.root = root;
		this.listenSocket = new ServerSocket(this.port);
		this.listenSocket.setSoTimeout(10000); //setTimeout time as  10 seconds
		this.setDaemon(true);
	}
	
	
	public void run() 
	{
		Socket socket = null;
		Thread thread = null;
		
		System.out.println("# AFtpServerThread have started " );
		System.out.println("# listening at port: " + this.port);
		
		//circularly listen to the connection and deal with it
		try
		{
			while(flag)
			{
				try 
				{
					socket = this.listenSocket.accept();
					thread = new AFtpConnectionThread(socket,root);
					//lthread.add(thread);
					thread.start();
				 } catch (IOException e) {
					// TODO Auto-generated catch block
					if(this.listenSocket != null)
						try {
							this.listenSocket.close();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
					e.printStackTrace();
					AFtpLogger.getInstance().log(e.getMessage());
				}
				
				//check out the dead thread and get rid of it
				/*try
				{
					int n = this.lthread.size();
					for(int i = 0; i < n; i++)
					{
						thread = (Thread) this.lthread.get(i);
						if(thread!= null && !thread.isAlive())
						{
						    this.lthread.remove(thread);
						    System.out.println("clear a thread ---" + thread.getName());
						}
					}
				}catch(Exception e)
				{
					e.printStackTrace();
					AFtpLogger.getInstance().log(e.getMessage());
				}*/
			}//end of while
		}//end of try
		catch(Exception e)
		{
			if(this.listenSocket != null)
				try {
					this.listenSocket.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
		}
		
	}
	
	
	public int getPort()
	{
		return port;
	}

	public static String getRoot() 
	{
		return root;
	}
}
